programming4us
           
 
 
Programming

Programming WCF Services : Data Contracts - Collections (part 1) - Concrete Collections & Custom Collections

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/16/2011 4:03:35 PM
In .NET, a collection is any type that supports the IEnumerable or IEnumerable<T> interface. All of the built-in collections in .NET, such as the array, the list, and the stack, support these interfaces. A data contract can include a collection as a data member, and a service contract can define operations that interact with a collection directly. Because .NET collections are .NET-specific, WCF cannot expose them in the service metadata, yet because they are so useful, WCF offers dedicated marshaling rules for collections.

Whenever you define a service operation that uses the collection interfaces IEnumerable<T>, IList<T>, or ICollection<T>, the resulting metadata always uses an array. For example, this service contract definition and implementation:

[ServiceContract]
interface IContactManager
{
[OperationContract]
IEnumerable<Contact> GetContacts();
...
}
class ContactManager : IContactManager
{
List<Contact> m_Contacts = new List<Contact>();

public IEnumerable<Contact> GetContacts()
{
return m_Contacts;
}
...
}

will still be exported as:

[ServiceContract]
interface IContactManager
{
[OperationContract]
Contact[] GetContacts();
}

1. Concrete Collections

If the collection in the contract is a concrete collection (not an interface) and is a serializable collection—that is, it is marked with the Serializable attribute but not with the DataContract attribute—WCF can normalize the collection automatically to an array of the collection’s type, provided the collection contains an Add() method with either one of these signatures:

public void Add(object obj); //Collection uses IEnumerable
public void Add(T item); //Collection uses IEnumerable<T>

For example, consider this contract definition:

[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(Contact contact);

[OperationContract]
List<Contact> GetContacts();
}

The list class is defined as:

public interface ICollection<T> : IEnumerable<T>
{...}
public interface IList<T> : ICollection<T>
{...}

[Serializable]
public class List<T> : IList<T>
{
public void Add(T item);
//More members
}

Because it is a valid collection and it has an Add() method, the resulting representation of the contract will be:

[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(Contact contact);

[OperationContract]
Contact[] GetContacts();
}

That is, a List<Contact> is marshaled as a Contact[]. The service may still return a List<Contact>, and yet the client will interact with an array, as shown in Example 1.

Example 1. Marshaling a list as an array
/////////////////////////// Service Side //////////////////////////////
[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(Contact contact);

[OperationContract]
List<Contact> GetContacts();
}
//Service implementation
class ContactManager : IContactManager
{
List<Contact> m_Contacts = new List<Contact>();

public void AddContact(Contact contact)
{
m_Contacts.Add(contact);
}

public List<Contact> GetContacts()
{
return m_Contacts;
}
}
/////////////////////////// Client Side //////////////////////////////
[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(Contact contact);

[OperationContract]
Contact[] GetContacts();
}
class ContactManagerClient : ClientBase<IContactManager>,IContactManager
{
public Contact[] GetContacts()
{
return Channel.GetContacts();
}
//More members
}
//Client code
ContactManagerClient proxy = new ContactManagerClient();
Contact[] contacts = proxy.GetContacts();
proxy.Close();


Note that while the collection must have the Add() method to be marshaled as an array, the collection need not implement the Add() method at all.

2. Custom Collections

It’s not just the built-in collections that can be marshaled automatically as arrays—any custom collection can abide by the same prerequisites and be marshaled as an array, as shown in Example 2. In this example, the collection MyCollection<string> is marshaled as a string[].

Example 2. Marshaling a custom collection as an array
/////////////////////////// Service Side //////////////////////////////
[Serializable]
public class MyCollection<T> : IEnumerable<T>
{
public void Add(T item)
{}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{...}
//Rest of the implementation
}
[ServiceContract]
interface IMyContract
{
[OperationContract]
MyCollection<string> GetCollection();
}

/////////////////////////// Client Side //////////////////////////////
[ServiceContract]
interface IMyContract
{
[OperationContract]
string[] GetCollection();
}


Other -----------------
- iPhone Programming : The Image Picker View Controller - Adding the Image Picker to the City Guide Application
- iPhone Programming : Other View Controllers - Modal View Controllers
- jQuery 1.3 : DOM Manipulation - Inserting new elements
- jQuery 1.3 : DOM Manipulation - Manipulating attributes
- DirectX 10 Game Programming : Adding the DirectX Libraries
- jQuery 1.3 : AJAX - Additional options
- jQuery 1.3 : AJAX and events & Security limitations
- jQuery 1.3 : AJAX - Keeping an eye on the request
- jQuery 1.3 : AJAX - Passing data to the server
- iPhone Programming : Other View Controllers - Tab Bar Applications
- iPhone Programming : Other View Controllers - Utility Applications
- iPhone Programming : Table-View-Based Applications - Adding a City View
- iPhone Programming : Table-View-Based Applications - Adding Navigation Controls to the Application
- iPad SDK : Popovers - Popover Preparations
- iPad SDK : Preparing Dudel for a New Tool (part 5) - Rendering Multiple Styles
- iPad SDK : Preparing Dudel for a New Tool (part 4) - Creating a New Drawable Class
- jQuery 1.3 : AJAX - Loading data on demand (part 3) - Loading an XML document
- jQuery 1.3 : AJAX - Loading data on demand (part 2) - Working with JavaScript objects
- jQuery 1.3 : AJAX - Loading data on demand (part 1) - Appending HTML
- Coding JavaScript for Mobile Browsers (part 13) - Zoom and rotate gestures
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us